package com.g1.gzbooksingle.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.g1.gzbooksingle.dto.BookInfoDTO;
import com.g1.gzbooksingle.mapper.DgAuthorInfoMapper;
import com.g1.gzbooksingle.mapper.DgBookInfoMapper;
import com.g1.gzbooksingle.mapper.DgIndexMenuMapper;
import com.g1.gzbooksingle.mapper.DgPublishInfoMapper;
import com.g1.gzbooksingle.model.DgAuthorInfo;
import com.g1.gzbooksingle.model.DgBookInfo;
import com.g1.gzbooksingle.model.DgIndexMenu;
import com.g1.gzbooksingle.model.DgPublishInfo;
import com.g1.gzbooksingle.service.IDgBookInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 图书信息表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-10-12
 */
@Slf4j
@Service
public class DgBookInfoServiceImpl extends ServiceImpl<DgBookInfoMapper, DgBookInfo> implements IDgBookInfoService {
    //图书
    @Autowired(required = false)
    private DgBookInfoMapper dgBookInfoMapper;
    //作者
    @Autowired(required = false)
    private DgAuthorInfoMapper dgAuthorInfoMapper;
    //出版社
    @Autowired(required = false)
    private DgPublishInfoMapper dgPublishInfoMapper;
    //分类
    @Autowired(required = false)
    private DgIndexMenuMapper dgIndexMenuMapper;

    @Value("${image-server}")
    private String imageServer;


    /**
     * 查询全部图书信息
     * @return
     */
    @Override
    public List<BookInfoDTO> getBookInfos() {
        //定义一个集合来装结果集
        List<BookInfoDTO> result = new ArrayList<>();
        //1、查询图书信息
        List<DgBookInfo> books = dgBookInfoMapper.selectAll();
        /**
         * 1	深度文案	1	1	深度文案	32.20	3a7257e7-0c5d-4482-ab75-4c3577dda56b.jpg	2021-10-04 12:02:45
         * 2	海底两万里	2	2	潜艇船长	16.00	ea40aa83-3a8c-4eb5-8ca2-e836e69642ec.jpeg	2021-10-09 11:47:37
         * 3	爱丽丝梦游仙境	1	1	爱丽丝	34.00	0804445c-5073-490b-884f-46dd6e621482.jpg	2021-10-12 17:11:13
         * 4	linux命令大全	1	1	命令大全	99.00	9db7ba77-199a-4cbb-9cf8-bf5bd490be18.jpg	2021-10-12 17:12:53
         *
         */
        //来个循环
        for (DgBookInfo bk : books){
            //2、通过作者id查询每本图书的作者
            DgAuthorInfo author = dgAuthorInfoMapper.selectById(bk.getAuthorId());
            //3、通过出版社id查询每本图书的出版社
            DgPublishInfo publish = dgPublishInfoMapper.selectById(bk.getPublishId());
            //3、通过出版社id查询每本图书的出版社
            DgIndexMenu indexMenu = dgIndexMenuMapper.selectById(bk.getIndexId());

            //这个DTO类就是页面的数据模型
            BookInfoDTO dto = new BookInfoDTO();
            //设置书名
            dto.setBookName(bk.getBookName());
            //设置作者名
            if (author!=null) {
                dto.setAuthorName(author.getAuthorName());
            }
            //设置出版社名
            if (publish!=null) {
                dto.setPublishName(publish.getPublishName());
            }
            // 分类名
            if (indexMenu!=null){
                dto.setIndexMenu(indexMenu.getName());
            }
            //设置图书简介
            dto.setDescption(bk.getDescption());
            //设置价格
            dto.setPrice(bk.getPrice());
            // 折扣价
            dto.setDiscountPrice(bk.getDiscountPrice());
            //设置图片路径
            dto.setImagePath(bk.getImagePath());
            //添加进集合结果集
            result.add(dto);
        }
        return result;
    }

    @Override
    public PageInfo<BookInfoDTO> getBookOrder(String orderColumn, Integer pageNum, Integer pageSize) {
        // 设置分页操作  startPage(int pageNum页码, int pageSize每页条数)
        PageHelper.startPage(pageNum,pageSize,orderColumn+" desc");
        List<DgBookInfo> dgBookInfos = dgBookInfoMapper.selectAll();
        List<BookInfoDTO> result = getBookInfoDTO(dgBookInfos);
        return new PageInfo<>(result);
    }

    @Override
    public PageInfo<BookInfoDTO> getBooksByIndexId(String indexIds, Integer pageNum, Integer pageSize) {
        // 设置分页操作  startPage(int pageNum页码, int pageSize每页条数)
        PageHelper.startPage(pageNum,pageSize);
        List<DgBookInfo> dgBookInfos = dgBookInfoMapper.selectByIndexId(indexIds);
        PageInfo<DgBookInfo> pd = new PageInfo<>(dgBookInfos);

        List<BookInfoDTO> result = getBookInfoDTO(dgBookInfos);
        PageInfo<BookInfoDTO> dtoPageInfo = new PageInfo<>(result);
        dtoPageInfo.setEndRow(pd.getEndRow());
        dtoPageInfo.setTotal(pd.getTotal());
        dtoPageInfo.setIsFirstPage(pd.isIsFirstPage());
        dtoPageInfo.setIsLastPage(pd.isIsLastPage());
        dtoPageInfo.setHasNextPage(pd.isHasNextPage());
        dtoPageInfo.setHasPreviousPage(pd.isHasPreviousPage());
        dtoPageInfo.setNavigateFirstPage(pd.getNavigateFirstPage());
        dtoPageInfo.setNavigateLastPage(pd.getNavigateLastPage());
        dtoPageInfo.setNavigatePages(pd.getNavigatePages());
        dtoPageInfo.setNavigatepageNums(pd.getNavigatepageNums());
        dtoPageInfo.setNextPage(pd.getNextPage());
        dtoPageInfo.setPageNum(pd.getPageNum());
        dtoPageInfo.setPages(pd.getPages());
        dtoPageInfo.setPageSize(pd.getPageSize());
        dtoPageInfo.setPrePage(pd.getPrePage());
        dtoPageInfo.setSize(pd.getSize());
        dtoPageInfo.setStartRow(pd.getStartRow());
        return dtoPageInfo;
    }



    public List<BookInfoDTO> getBookInfoDTO(List<DgBookInfo> dgBookInfos){
        List<BookInfoDTO> result = new ArrayList<>();
        for (DgBookInfo bookInfo : dgBookInfos){
            //2、通过作者id查询每本图书的作者
            DgAuthorInfo author = dgAuthorInfoMapper.selectById(bookInfo.getAuthorId());
            //3、通过出版社id查询每本图书的出版社
            DgPublishInfo publish = dgPublishInfoMapper.selectById(bookInfo.getPublishId());
            //
            DgIndexMenu indexMenu = dgIndexMenuMapper.selectById(bookInfo.getIndexId());

            //这个DTO类就是页面的数据模型
            BookInfoDTO dto = new BookInfoDTO();
            //设置书名
            dto.setBookName(bookInfo.getBookName());
            //设置作者名
            if (author!=null) {
                dto.setAuthorName(author.getAuthorName());
            }
            //设置出版社名
            if (publish!=null) {
                dto.setPublishName(publish.getPublishName());
            }
            //设置图书简介
            dto.setDescption(bookInfo.getDescption());
            // 分类名
            if (indexMenu!=null){
                dto.setIndexMenu(indexMenu.getName());
            }
            // 设置价格
            dto.setPrice(bookInfo.getPrice());
            // 折扣价
            dto.setDiscountPrice(bookInfo.getDiscountPrice());
            //设置图片路径
            dto.setImagePath(bookInfo.getImagePath());
            //添加进集合结果集
            result.add(dto);
        }
        return result;
    }


}
